home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / freemacs.arc / FILES.ASM < prev    next >
Assembly Source File  |  1988-03-17  |  5KB  |  270 lines

  1. ;History:2
  2.     page    ,132
  3.  
  4.     .xlist
  5.  
  6.     include    memory.def
  7.  
  8.     .list
  9.  
  10. bufseg    segment    public
  11.     extrn    toptop: word
  12.     extrn    topbot: word
  13.     extrn    bottop: word
  14.     extrn    botbot: word
  15.     extrn    margin: abs
  16.     extrn    linecount: word
  17.     extrn    linesbefore: word
  18.     extrn    buffer_modified: byte
  19. bufseg    ends
  20.  
  21.  
  22. data    segment byte public
  23.  
  24.     extrn    textseg: word
  25.  
  26. data    ends
  27.  
  28.  
  29. code    segment byte public
  30.  
  31.     assume    cs:code, ds:data, es:data
  32.  
  33.     extrn    count_lines: near
  34.     extrn    read_mark$: near
  35.     extrn    adjust_marks_ins: near
  36.     extrn    paint_window: near
  37.  
  38.  
  39.     public    write_file
  40. write_file:
  41. ;enter with si->filename desired, al=mark.
  42. ;exit with al=0 if ok, al=1 if disk full, or al=2 if directory full.
  43.     push    si            ;save a pointer to the filename.
  44.  
  45.     push    ax            ;save the mark to write to.
  46.     mov    dx,si
  47.     mov    cx,0
  48.     mov    ah,3ch            ;create file.
  49.     int    21h
  50.     pop    bx            ;get the mark back
  51.     jc    write_2_5        ;if any problem, report dir full.
  52.     xchg    bx,ax            ;put the mark in ax, handle in bx.
  53.  
  54.     push    ds
  55.     mov    ds,textseg        ;read_mark$ assumes ds:textseg
  56.     push    bx
  57.     call    read_mark$
  58.     pop    bx
  59.     mov    dx,si            ;set the disk transfer address
  60.     mov    ah,40h            ;write out what they requested.
  61.     int    21h
  62.     pop    ds
  63.     jc    write_2_4        ;if any problems, disk full.
  64.  
  65.     cmp    ax,cx            ;did we write the whole thing?
  66.     jne    write_2_4        ;no - disk full.
  67.  
  68. ;close the file.
  69.     mov    ah,3eh            ;close file.
  70.     int    21h
  71.     jc    write_2_4        ;if any problems, say disk full.
  72.     pop    si
  73.     mov    al,0
  74.     ret
  75. write_2_4:
  76.     mov    ah,3eh            ;close file.
  77.     int    21h
  78.     pop    si
  79.     mov    dx,si            ;delete their file.
  80.     mov    ah,41h
  81.     int    21h
  82.     mov    al,1
  83.     ret
  84. write_2_5:
  85.     pop    si
  86.     mov    al,2
  87.     ret
  88.  
  89.  
  90.     public    read_file
  91. read_file:
  92. ;enter with si->filename desired.
  93. ;exit with al=0 if ok, al=1 if file too large, or al=2 if file not found.
  94.     mov    dx,si
  95.     mov    ax,3d00h        ;open for reading.
  96.     int    21h
  97.     jc    read_2_5
  98.     mov    bx,ax
  99.  
  100.     mov    ax,4202h        ;get the file's size.
  101.     mov    cx,0
  102.     mov    dx,cx
  103.     int    21h
  104.     mov    cx,ax            ;remember the file's size lobyte.
  105.  
  106.     push    ds
  107.     mov    ds,textseg
  108.     assume    ds:bufseg
  109.     mov    ax,bottop        ;get size of available memory.
  110.     sub    ax,topbot
  111.     pop    ds
  112.     assume    ds:data
  113.  
  114. ;check the size of the file (dx:cx) against buffer size (ax)
  115.     or    dx,dx            ;is file too big?
  116.     jne    read_2_2
  117.     cmp    ax,margin
  118.     jb    read_2_2        ;not enough memory.
  119.     sub    ax,margin
  120.     cmp    ax,cx            ;enough memory to read the file in?
  121.     jb    read_2_2        ;no.
  122.  
  123. ;if the file's empty, don't try to read it in.
  124.     jcxz    read_2_7
  125.  
  126. ;seek to the beginning again.
  127.  
  128.     push    cx
  129.     mov    ax,4200h        ;rewind the file.
  130.     mov    cx,0
  131.     mov    dx,cx
  132.     int    21h
  133.     pop    cx
  134.  
  135. ;read the file in.
  136.  
  137.     push    ds
  138.     mov    ds,textseg
  139.     assume    ds:bufseg
  140.     mov    dx,topbot        ;read the file before the cursor.
  141.     mov    ah,3fh
  142.     int    21h
  143.     pop    ds
  144.     assume    ds:data
  145.     jc    read_2_4        ;problem with read - give up.
  146.  
  147.     cmp    cx,ax            ;compare the amount desired again the amount read.
  148.     pushf
  149.     mov    cx,ax            ;adjust for the amount read.
  150.     push    bx            ;preserve the file handle.
  151.     call    read_adjust
  152.     pop    bx
  153.     popf
  154.     jnz    read_2_4        ;if any not read, give up.
  155.  
  156.  
  157. ;close the file.
  158. read_2_7:
  159.     mov    ah,3eh            ;close the file.
  160.     int    21h
  161.     jc    read_2_4        ;if any trouble, give up.
  162.     mov    al,0
  163.     jmp    short read_2_exit
  164. read_2_2:
  165.     mov    al,1            ;not enough memory.
  166.     jmp    short read_2_exit
  167. read_2_4:
  168.     mov    ah,3eh            ;close the file.
  169.     int    21h
  170.     mov    al,3            ;other read error.
  171.     jmp    short read_2_exit
  172. read_2_5:
  173.     mov    al,2            ;file not found.
  174. read_2_exit:
  175.     ret
  176.  
  177.  
  178. read_adjust:
  179. ;update topbot, line counters.
  180.     push    cx            ;save count
  181.     mov    ds,textseg
  182.     assume    ds:bufseg
  183.     mov    ax,cx
  184.     call    adjust_marks_ins
  185.     mov    di,topbot
  186.     push    es
  187.     pop    ds
  188.     assume    ds:data
  189.     pop    cx            ;get the count back.
  190.     push    cx
  191.     call    count_lines        ;count NEWLINES in text we just read in.
  192.     mov    ds,textseg
  193.     assume    ds:bufseg
  194.     add    linesbefore,bx        ;the text is before the cursor, so add
  195.     add    linecount,bx
  196.     mov    buffer_modified,1
  197.     pop    cx            ;restore count
  198.     add    topbot,cx        ;add the amount of file that was read in.
  199.     push    es
  200.     pop    ds
  201.     assume    ds:data
  202.     call    paint_window
  203.     ret
  204.  
  205.  
  206.     public    compute_free
  207. compute_free:
  208. ;enter with dl=drive.
  209. ;exit with cy if invalid drive, or nc and:
  210. ;  ax=sectors/cluster,
  211. ;  bx=free clusters,
  212. ;  cx=bytes/sector,
  213. ;  dx=total clusters.
  214.     mov    ah,30h            ;get the version number
  215.     int    21h
  216.     cmp    al,2            ;al<2 for versions 1.?
  217.     jb    compute_free_5
  218.     mov    ah,36h            ;get disk stats for version 2.0+
  219.     int    21h            ;dl already set
  220.     cmp    ax,-1            ;valid drive?
  221.     jne    compute_free_4        ;yes.
  222.     push    ds
  223. compute_free_6:
  224.     pop    ds
  225.     stc
  226.     ret
  227. compute_free_5:
  228.     push    ds
  229.     mov    ah,1ch            ;get the fat.
  230.     int    21h
  231.     or    bx,bx
  232.     je    compute_free_6
  233.     mov    ah,0
  234.     push    ax            ;save the sectors/cluster
  235.     push    cx            ;save the bytes/sector
  236.     mov    cx,dx            ;we need the count in cx.
  237.     xor    ax,ax            ;start with no free clusters.
  238.     mov    si,2            ;start at the first cluster.
  239. compute_free_1:
  240.     mov    di,si
  241.     shr    di,1
  242.     add    di,si
  243.     mov    di,[bx+di]
  244.     test    si,001
  245.     jz    compute_free_2
  246.     shr    di,1
  247.     shr    di,1
  248.     shr    di,1
  249.     shr    di,1
  250. compute_free_2:
  251.     and    di,0fffh        ;seperate out the bits.
  252.     jnz    compute_free_3        ;free? no.
  253.     inc    ax            ;count a free cluster.
  254. compute_free_3:
  255.     inc    si            ;go to the next.
  256.     loop    compute_free_1        ;until we've looked at them all.
  257.     mov    bx,ax            ;save the free clusters in bx.
  258.     pop    cx            ;restore bytes/sector
  259.     pop    ax            ;restore sectors/cluster.
  260.     pop    ds
  261. compute_free_4:
  262.     clc
  263.     ret
  264.  
  265.  
  266. code    ends
  267.  
  268.     end
  269.  
  270.